package webui

import (
	"dingbot/config"
	"dingbot/errhelp"

	"github.com/gofiber/fiber/v2"
	"github.com/gofiber/template/html"
)

var (
	Server *fiber.App
)

func Run() {
	t_en := html.New("./plugin_web", ".html")
	Server = fiber.New(fiber.Config{
		ErrorHandler: func(c *fiber.Ctx, err error) error {
			return c.JSON(fiber.Map{"message": err.Error(), "code": -1})
		},
		Views: t_en,
	})

	Server.Static("/", "./web")
	Server.Post("/api/login", LoginRoute)
	Server.Post("/api/admin_info", AdminInfoRoute)
	Server.Post("/api/auth", AuthRoute)
	Server.Post("/api/plugin/get", GetPluginsRoute)
	Server.Get("/api/plugin/write", GetPluginWritable)
	Server.Get("/api/plugin/save", SavePlugin)
	Server.Get("/api/plugin/getjson", GetPluginJsonRoute)
	Server.Get("/plugin/write", WritePluginRoute)
	Server.Static("/plugin_web", "./plugin_web")

	err := Server.Listen(":" + config.GetConfig().Port.WebUI)
	errhelp.Abort(err, "Start WebUI Server")
}
